import { createRouter, createWebHistory } from "vue-router";
import Layout from "@/Layout/index.vue";
import { isLogin } from "@/utils/user";
const routes = [
  {
    path: "/",
    name: "Layout",
    meta: {
      roles: "*",
    },
    children: [
      {
        path: "/",
        redirect: "/dashBoard",
      },
      {
        path: "dashBoard",
        name: "dashBoard",
        meta: {
          roles: "*",
          paths: [
            {
              label: "首页",
            },
          ],
        },
        component: () => import("@/views/DashBoard/index.vue"),
      },
      {
        path: "itemLists",
        name: "itemLists",
        meta: {
          roles: "*",
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "商品管理",
            },
          ],
        },
        component: () => import("@/views/ItemLists/index.vue"),
      },
      {
        path: "itemAdd",
        name: "itemAdd",
        meta: {
          roles: ["admin", "a", "b"],
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "商品管理",
              path: "/itemLists",
            },
            {
              label: "新增商品",
            },
          ],
        },
        component: () => import("@/views/ItemAdd/index.vue"),
      },
      {
        path: "itemUpdate",
        name: "itemUpdate",
        meta: {
          roles: ["admin", "a", "b"],
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "商品管理",
              path: "/itemLists",
            },
            {
              label: "修改商品",
            },
          ],
        },
        component: () => import("@/views/ItemUpdate/index.vue"),
      },
      {
        path: "cateLists",
        name: "cateLists",
        meta: {
          roles: ["admin", "a", "b"],
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "商品分类管理",
            },
          ],
        },
        component: () => import("@/views/CateLists/index.vue"),
      },
      {
        path: "orderLists",
        name: "orderLists",
        meta: {
          roles: "*",
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "订单列表",
            },
          ],
        },
        component: () => import("@/views/OrderLists/index.vue"),
      },
      {
        path: "setting",
        name: "setting",
        meta: {
          roles: ["a", "b"],
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "设置",
            },
          ],
        },
        component: () => import("@/views/SettingPage/index.vue"),
      },
      {
        path: "userInfo",
        name: "userInfo",

        meta: {
          roles: "*",
          paths: [
            {
              path: "/dashBoard",
              label: "首页",
            },
            {
              label: "个人中心",
            },
          ],
        },
        component: () => import("@/views/UserInfo/index.vue"),
      },
    ],
    component: Layout,
  },
  {
    path: "/login",
    meta: {
      roles: "*",
    },
    name: "Login",
    component: () => import("@/views/LoginPage/index.vue"),
  },
  {
    path: "/noauth",
    meta: {
      roles: "*",
    },
    name: "noauth",
    component: () => import("@/views/NoAuth/index.vue"),
  },
  {
    path: "/:pathMatch(.*)*",
    meta: {
      roles: "*",
    },
    name: "404",
    component: () => import("@/views/NotFound"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 添加拦截器 做鉴权
router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    if (isLogin()) {
      next("/dashBoard");
    } else {
      next();
    }
  } else {
    // 不是登录页
    if (isLogin()) {
      // 判断是否 有权限访问该路由
      const role = localStorage.getItem("role");
      if (to.meta.roles === "*" || to.meta.roles.includes(role)) {
        next();
      } else {
        // 去没有权限路由
        next("/noauth");
      }
    } else {
      next("/login");
    }
  }
});
export default router;
